package com.restkeeper.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.restkeeper.entity.DyUserTimes;
import com.restkeeper.service.DyUserTimesService;
import com.restkeeper.utils.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping("/dyUserTimes")
public class DyUserTimesController {

    @Autowired
    private DyUserTimesService dyUserTimesService;

    /**
     * 分页查询
     */
    @GetMapping("/page")
    public Result page(@RequestParam(defaultValue = "1") long current,
                       @RequestParam(defaultValue = "10") long size) {
        Page<DyUserTimes> page = new Page<>(current, size);
        return Result.ok(dyUserTimesService.page(page, new QueryWrapper<>()));
    }

    /**
     * 添加用户次数记录
     */
    @PostMapping
    public Result add(@RequestBody DyUserTimes dyUserTimes) {
        return dyUserTimesService.save(dyUserTimes) ? Result.ok() : Result.error();
    }

    /**
     * 修改用户次数记录
     */
    @PutMapping
    public Result update(@RequestBody DyUserTimes dyUserTimes) {
        return dyUserTimesService.updateById(dyUserTimes) ? Result.ok() : Result.error();
    }

    /**
     * 删除用户次数记录
     */
    @DeleteMapping("/{id}")
    public Result delete(@PathVariable Long id) {
        return dyUserTimesService.removeById(id) ? Result.ok() : Result.error();
    }
}